﻿using System;
using System.Web.UI.WebControls;
using ISHotelApp.Database;

namespace ISHotel_ASP.presentation
{
    public partial class PokojForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPage();
        }

        protected void ButtonVlozPokoj_Click(object sender, EventArgs e)
        {
            Pokoj newPokoj = new Pokoj();
            newPokoj.id_pokoje = Convert.ToInt32(TextBoxIDPokoje.Text);
            newPokoj.popis_pokoje = TextBoxPopis.Text;
            newPokoj.kapacita = Convert.ToInt32(DropDownListKapacita.SelectedValue);
            newPokoj.cena_pokoje = Convert.ToInt32(TextBoxCena.Text);

            PokojTable addSourceRecord = new PokojTable();
            addSourceRecord.Insert(newPokoj);

            Response.Redirect(Request.RawUrl);
        }

        public void button_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "DeletePokoj")
            {
                PokojTable delSourceRecord = new PokojTable();
                
                try
                {
                    delSourceRecord.Delete(Convert.ToInt32(e.CommandArgument));
                }
                catch (Exception ex)
                {
                    LabelErr.Text = "Pokoj nelze smazat, je již obsažen v objednávkách.";
                }
                if (LabelErr.Text == "")
                {
                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        protected void PokojDetail_OnUpdated(object sender, EventArgs e)
        {
            //GridViewPokoj.DataBind();
            GridView1.DataBind();
            DetailsView1.DataBind();
        }

        protected void PokojDetail_Update(object sender, EventArgs e)
        {
            //GridViewPokoj.DataBind();
            GridView1.DataBind();
            DetailsView1.DataBind();
        }

        //Nastavi pocet stranek
        private void SetPage()
        {
            ISHotelApp.Database.PokojTable db = new ISHotelApp.Database.PokojTable();
            int pocet = db.CountRow();
            int stranka = pocet / Convert.ToInt32(DropPocetZaz.SelectedValue);
            int zbytek = pocet % Convert.ToInt32(DropPocetZaz.SelectedValue);
            if (zbytek != 0)
            {
                stranka++;
            }

            LabelLast.Text = stranka.ToString();


        }

        protected void DropPocetZaz_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextGotoPage.Text = "1";
            SetPage();
            GridView1.DataBind();
        }

        protected void ButtonJdiNaStr_Click(object sender, EventArgs e)
        {
            int i = 1;
            try { i = Convert.ToInt32(TextGotoPage.Text); }
            catch { TextGotoPage.Text = "1"; LblStatus.Text = "Zadejte číslo"; }

            if (i > Convert.ToInt32(LabelLast.Text))
            {
                LabelCurentPage.Text = LabelLast.Text;

            }
            else
            {
                LabelCurentPage.Text = TextGotoPage.Text;
            }

            GridView1.DataBind();
        }

        protected void ButtonMinus_Click(object sender, EventArgs e)
        {
            int curPage = Convert.ToInt32(LabelCurentPage.Text);
            if (curPage > 1)
            {
                curPage--;
                TextGotoPage.Text = curPage.ToString();
                LabelCurentPage.Text = curPage.ToString();
                GridView1.DataBind();
            }
        }

        protected void ButtonPlus_Click(object sender, EventArgs e)
        {
            int curPage = Convert.ToInt32(LabelCurentPage.Text);
            int maxPage = Convert.ToInt32(LabelLast.Text);
            if (curPage < maxPage)
            {
                curPage++;
                TextGotoPage.Text = curPage.ToString();
                LabelCurentPage.Text = curPage.ToString();
                GridView1.DataBind();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = GridView1.SelectedIndex;
        }
    }
}